Writing WSH scripts If you've
written scripts for Internet Explorer or the Office
applications, then you'll have a good idea of how WSH
scripts are constructed - although there are plenty of
differences to keep you on your toes!
Writing scripts for the WSH is not
a trivial process - whatever language you use, scripting
is ultimately about manipulating the properties and
methods of objects exposed in the WSH object model, and
in the object models of any applications you control.
Even something as basic as writing a text file can
involve creating a file object variable and calling two
of its methods to save the file and store data in it. WSH
scripting is totally different from writing MS-DOS batch
files.
A knowledge of VBScript or
JavaScript will put you 75% along the way to successful
WSH scripting, but bear in mind that WSH properties and
methods may not be same as the ones you're used to in,
say, IE4.
For example, to pop up a message box in IE4, you might
write this JavaScript statement:
alert("Hello
World")
but in a JavaScript routine for use
with the WSH, you'd say:
WScript.Echo("Hello
World").
Another difference is that script
files don't need the equivalent of a <SCRIPT tag or
LANGUAGE identifier - the file name extension (.js or
.vbs) tells WSH what language is used.
Here's a simple JScript
(JavaScript) script which creates a new file in the
current disk folder, and writes a line of text to it:
var created_date =
new Date()
var fs =
WScript.CreateObject("Scripting.FileSystemObject")
var a = fs.CreateTextFile("jstestfile.txt",
"True")
a.WriteLine("This is a test file, written at
"+created_date)
a.Close()
For details of the WSH object model
and other WSH documentation, see Microsoft's scripting developer web site.
|